package com.example.chasshelper;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.chasshelper.adapter.SubjectQueryResultAdapter;
import com.example.chasshelper.adapter.SubjectStuAdapter;
import com.example.chasshelper.dao.ClassroomDao;
import com.example.chasshelper.dao.CourseDao;
import com.example.chasshelper.dao.MessageDao;
import com.example.chasshelper.dao.RegistrationDao;
import com.example.chasshelper.dao.SolutionDao;
import com.example.chasshelper.dao.StudentDao;
import com.example.chasshelper.dao.TeacherDao;
import com.example.chasshelper.dao.TestDao;
import com.example.chasshelper.model.Course;
import com.example.chasshelper.model.Student;
import com.example.chasshelper.model.Teacher;
import com.example.chasshelper.utils.DaoUtils;

import java.util.ArrayList;

public class SubjectQueryResultActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = SubjectQueryResultActivity.class.getSimpleName();
    private final Activity mContext = SubjectQueryResultActivity.this;
    //控件
    private Button btn_back;
    private RecyclerView rv_subject_result_stu;
    private EditText et_factor;
    private ImageView iv_search;
    //dao
    private TeacherDao teacherDao= DaoUtils.getTeacherDao();
    private StudentDao studentDao=DaoUtils.getStudentDao();
    private CourseDao courseDao=DaoUtils.getCourseDao();
    private ClassroomDao classroomDao=DaoUtils.getClassroomDao();
    private RegistrationDao registrationDao=DaoUtils.getRegistrationDao();
    private SolutionDao solutionDao = DaoUtils.getSolutionDao();
    private MessageDao messageDao = DaoUtils.getMessageDao();
    private TestDao testDao = DaoUtils.getTestDao();
    //数据
    private Student student;
    private ArrayList<Course> queryResult;

    //adapter
    private SubjectQueryResultAdapter subjectQueryResultAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_subject_query_result);
        //线程
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        //初始化界面
        initUI();
        //初始化数据
        initData();

    }
    private void initUI() {
        //绑定控件
        btn_back = findViewById(R.id.btn_back);
        rv_subject_result_stu = findViewById(R.id.rv_subject_result_stu);
        et_factor = findViewById(R.id.et_factor);
        iv_search = findViewById(R.id.iv_search);
        //点击事件
        btn_back.setOnClickListener(this);
        iv_search.setOnClickListener(this);
    }

    private void initData() {
        //接收数据
        student = (Student) getIntent().getSerializableExtra("student");
        //查询数据
        queryResult= (ArrayList<Course>) courseDao.findAll();
        //adapter实例化
        subjectQueryResultAdapter = new SubjectQueryResultAdapter(mContext,queryResult,student);
        rv_subject_result_stu.setLayoutManager(new LinearLayoutManager(mContext));
        rv_subject_result_stu.setAdapter(subjectQueryResultAdapter);

    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.btn_back://返回
                finish();
                break;
            case R.id.iv_search:
                queryResult = (ArrayList<Course>) courseDao.findByIdOrCourseNameLike(et_factor.getText().toString());
                subjectQueryResultAdapter.setList(queryResult);
                subjectQueryResultAdapter.notifyDataSetChanged();
                break;
        }
    }

}